<?php
namespace App\Controllers\Product\Page;

use App\Libs\Goods\Goods;
use \App\Models\Goods\SpecialModel;
use App\Models\Goods\Goods as GoodsModel;
use App\Libs\Helper\CommonHelper;
/**
*	专题
*
*/
class Special extends  \App\Libs\Base\BaseTwigAction 
{
    protected function beforeRun() {
        parent::beforeRun();
	//	 $this->needLogin = TRUE;
    }
	
	public function execute() {
		$sid = (int)$this->request->get("sid");
		$goods = SpecialModel::find(array(
			'id=:sid:',
			'bind'=>array('sid'=>$sid)
		));
		if( !empty( $goods ) ){
			$goods = $goods->toArray();
			if( !empty( $goods['gdata'] )){
                $goodsList = array_values(json_decode($goods['gdata'],true));

                foreach ($goodsList as $key => $val){
                    $goodsIDArray[] = $val['id'];
                }
                $matchGoods = GoodsModel::findAll([
                    'id in ({ddd:array})',
                    'bind' => ['ddd'=> $goodsIDArray],
                    'columns' => 'id,goods_sold'
                ])->toArray();
                $transGoods = CommonHelper::setColToKey($matchGoods,'id');
                foreach ($goodsList as $key => $val){
                    if(isset($transGoods[$goodsList[$key]['id']])){
                        $goodsList[$key]['goods_sold'] = $transGoods[$goodsList[$key]['id']]['goods_sold'];
                    }
                    $goodsList[$key]['name_full'] = $goodsList[$key]['name'];
                    $goodsList[$key]['name'] = mb_substr($goodsList[$key]['name'],0,10).'...';
                }
                $goods['gdata'] = $goodsList;
			}else{
				$goods['gdata'] = [];
			}
		}else{
			$goods['gdata'] = [];
			$goods['url'] = '';
		}
		$this->addParam('info' , $goods);
		return parent::execute();
	}
}
